/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zdb.demo.flink.batch;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.io.TextInputFormat;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.fs.Path;
import org.apache.flink.util.Collector;

import java.util.stream.Stream;

/**
 * Skeleton for a Flink Batch Job.
 *
 * <p>For a tutorial how to write a Flink batch application, check the
 * tutorials and examples on the <a href="http://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution,
 * change the main class in the POM.xml file to this class (simply search for 'mainClass')
 * and run 'mvn clean package' on the command line.
 */
public class BatchJob {

	public static void main(String[] args) throws Exception {
		// set up the batch execution environment
        Configuration conf = new Configuration(){{
            setInteger("rest.port", 9191);
            setBoolean("local.start-webserver", true);
        }};
        final ExecutionEnvironment env = ExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);

//        DataSource<String> dataset1 = env.readTextFile("file:///Users/zhudebin/Documents/iworkspace/antlr4-demo/");
//        DataSet<String> dataset = env.readFile(new TextInputFormat(new Path("aa")),"file:///Users/zhudebin/Documents/iworkspace/antlr4-demo/");
        DataSet<String> dataset = env.readTextFile("file:///E:/downloads/yunpan/bash脚本编程/bash脚本第五课/awk.txt");
//        DataSet<String> dataset = env.fromElements("apache flink", "hello world", "flink sql");
        dataset.flatMap((String value, Collector<Tuple2<String, Integer>> out) -> {
                Stream.of(value.split("\\W+")).forEach(word -> out.collect(new Tuple2<>(word, 1)));
            }).returns(Types.TUPLE(Types.STRING, Types.INT))
                .groupBy(0)
                .sum(1).print();

		// execute program
//		env.execute("Flink Batch Java API Skeleton");
	}
}
